package com.example.csmall.product.controller;

import com.example.csmall.product.ex.ServiceException;
import com.example.csmall.product.pojo.dto.AlbumAddNewDTO;
import com.example.csmall.product.pojo.dto.AlbumUpdateDTO;
import com.example.csmall.product.pojo.vo.AlbumListItemVO;
import com.example.csmall.product.pojo.vo.AlbumStandardVO;
import com.example.csmall.product.service.AlbumService;
import com.example.csmall.product.web.JsonResult;
import com.example.csmall.product.web.ServiceCode;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty;
import java.util.List;

/**
 * 添加此诸结，框架才会认为这个类是一个控制器
 */
@RestController
@RequestMapping("/albums")
@Validated
@Api(tags = "01 相册管理模块")
public class AlbumController {

    @Autowired
    AlbumService albumService;

    /**
     * 向相册表中添加数据
     * @param albumAddNewDTO 请求带来的数据
     * @return 执行结果.
     */
    @PostMapping("/add-new")
    @ApiOperation("添加相册")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> addNew(@Valid AlbumAddNewDTO albumAddNewDTO) {
        albumService.addNew(albumAddNewDTO);
        JsonResult<Void> jr = new JsonResult();
        jr.setState(ServiceCode.OK.getValue());
        jr.setMessage("相册添加成功");
        return jr;
    }

    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("删除相册")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id",value = "删除相册的ID",dataType = "long")
    public JsonResult<Void> deleteById(@Range(max = 150,min = 0,message = "删除相册失败,id无效") @PathVariable Long id){
        albumService.deleteById(id);
        return JsonResult.ok();
    }

    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改")
    @ApiOperationSupport(order = 240)
    @ApiImplicitParam(name = "id",value = "修改的id",dataType = "long")
    public JsonResult<Void> updateInfoById(@PathVariable Long id, AlbumUpdateDTO albumUpdateDTO){
        albumService.updateInfoById(id,albumUpdateDTO);
        return JsonResult.ok();
    }

    @GetMapping("/{id:[0-9]+}")
    @ApiOperation("ID查询数据")
    @ApiOperationSupport(order = 421)
    public JsonResult<AlbumStandardVO> getStandardById(@PathVariable Long id){
        AlbumStandardVO standardById = albumService.getStandardById(id);
        return JsonResult.ok(standardById);
    }

    @GetMapping("")
    @ApiOperation("相册列表")
    @ApiOperationSupport(order = 420)
    public JsonResult<List<AlbumListItemVO>> list(){
        List<AlbumListItemVO> list = albumService.list();
        return JsonResult.ok(list);
    }

}
